program Heap;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  arr : array of integer;
  i, t, n, m: Integer;

procedure Heapify (num : integer);
var
  x, a, b : Integer;
begin
  while (1=1) do
  begin
  x := arr[num];
  a := 2000000000;
  b := a;
  if (2*num + 1 < N) then
    a := arr[2*num+1];
  if (2*num  < N) then
    b := arr[2*num];
  if (a <= b) and (a < x) then
  begin
    arr[num] := a;
    arr[2*num+1] := x;
    num := 2*num + 1;
  end else
  if (b <= a) and (b < x) then
  begin
    arr[num] := b;
    arr[2*num] := x;
    num := 2*num;
  end else
    break;
  end;
end;


begin
  Readln (n);
  SetLength (arr, n);
  Randomize;
  for i := 0 to n-1 do
  begin
    arr[i] := Random (n*n);
    write (arr[i], ' ');
  end;
  readln;


  for i := N-1 downto 0 do
    Heapify(i);

  M := N;
  for i := 0 to M-1 do
  begin
    t := arr[0];
    arr[0] := arr[N-1];
    arr[N-1] := t;
    dec (N);
    Heapify(0);
  end;
  N := M;



  for i := 0 to n-1 do
     write (arr[i], ' ');
     Readln;

end.






